package com.hhtc.jsj.common.mybatis.generator;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.SQLException;

public class MybatisGenerator {
    public static void main(String[] args) throws SQLException {

        //1. 全局配置
        GlobalConfig config = new GlobalConfig();
        config.setActiveRecord(true)
                .setAuthor("ty")
                .setOutputDir("D:\\project-work\\local\\jsj\\src\\main\\java")
                .setFileOverride(true)
                .setIdType(IdType.AUTO)
                .setSwagger2(true)
                .setServiceName("%sService")
                .setBaseResultMap(true)
                .setBaseColumnList(true);


        //2. 数据源配置
        DataSourceConfig dsConfig  = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://localhost:3306/hhtc?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC")
                .setUsername("root")
                .setPassword("root");

        //3. 策略配置globalConfiguration中
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
        //是否生成实体时，生成字段配置 即在字段属性上加上@TableField("")注解
                .setEntityTableFieldAnnotationEnable(true)
                .setInclude("user");

        //4. 包名策略配置
        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent("com.hhtc.jsj")
                .setMapper("mapper")//dao
                .setService("service")//servcie
                .setController("controller")//controller
                .setServiceImpl("service.impl")
                .setEntity("entity")
                .setXml("mapper.xml");//mapper.xml
        //5. 整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(config)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(pkConfig)
                .setTemplateEngine(new FreemarkerTemplateEngine());
        ag.execute();
    }


}
